<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class AdminShopType extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_type';

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'p_text',
    ];

    /**
     * User: chenniang
     * Date: 2019-09-28 21:07
     * @param $value
     * @param $data
     * @return mixed
     * descption:转换图片
     */
    public function getCoverAttr($value,$data){
        $data = transImagesOne($data,['cover']);
        return $data['cover'];
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 父级名字
     */

    public function getPTextAttr($value,$data){
       if($data['pid']!=0){
           $p_data = $this->where('id','=',$data['pid'])->value('title');
       }else{
           $p_data = '顶级分类';
       }
       return $p_data;
    }

    /**
     * 获取商品列表
     */
    public function cateList($dis){
        $dis[]= ['status','>','-1'];
        $data =$this->where($dis)->order('top desc,id desc')->select()->toArray();
        $data = $this->getTree($data,0);
        return $data;
    }
    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     */
    public function getTree($data, $pId){
         $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTree($data, $v['id']);
                    if(empty($v['children'])){
                        unset($v['children']);
                    }
                    $tree[]   = $v;
                }
            }
        }
        return $tree;
    }

    /**
     * @param $id
     * 回显
     */
    public function reset($id,&$data=array()){
        $cate   = Db::name($this->name)->where(['id'=>$id])->find();
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-09-10 19:05
     * @param $ids
     * @return array
     * descption:回显福包分类
     */
    public function limitCate($ids){
        $cate_id = $this->where('id','in',$ids)->status(-1)->column('id');
        $data    = array();
        if(!empty($cate_id)){
            foreach ($cate_id as $v){
                $data [] = $this->reset($v);
            }
        }
        return $data;
    }

    /**
     * 修改分类
     */
    public function cateUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }

    /**
     *
     * 添加分类
     */

    public function cateAdd($data){
        $data['create_time'] = time();
        $res = $this->insert($data);
        return $res;
    }

    /**
     * 获取分类详情
     */

    public function cateInfo($dis){
        $data = $this->where($dis)->find();
        return $data;
    }

    /**
     * 查询分类
     */

    public function catSelect($dis=array()){
        $data = $this->where($dis)->status(-1)->order('top desc,id desc')->select();
        return $data;
    }

    /**
     * 排序好的分类
     */

    public function catSortSelect($dis=array()){
        $data = $this->where($dis)->status(-1)->order('top desc,id desc')->select()->toArray();
        return $this->getTree($data,0);
    }

    /**
     * @param $id
     * @return mixed
     * 检查是否是一级分类
     */

    public function checkOne($id){
        $pid = $this->where([['id','=',$id]])->value('pid');
        return $pid;
    }

    /**
     *
     * 获取下级分类
     */

    public function getSonCate($id){
        $data = $this->where([['pid','=',$id]])->select()->toArray();
        return $data;
    }


    /**
     * 获取下级分类id
     *
     */
    public function getSonCatId($id){
        $pid    = $this->checkOne($id);
        $son_id = array();
        if($pid==0){
            $son_id = $this->where([['pid','=',$id]])->column('id');
        }
        $son_id[] = $id;
        return $son_id;
    }

    /**
     * 获取分类名
     */
    public function cateTitle($dis){
        $title = $this->where($dis)->status(-1)->value('title');
        return $title;
    }



}